﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    WinJS.strictProcessing();

    var locationHost="ms-appx-web://" + document.location.host;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll().then(
                function (e) {
                    //hacemos visible la barra de aplicación
                    var appBar = document.querySelector("#appBar").winControl;
                    appBar.show();

                    //inicializamos el comando de posición
                    var locationButton = document.querySelector("#cmdLocation");
                    locationButton.addEventListener("click",
                        function (e) {
                            //activamos el sistema de localización
                            var location = new Windows.Devices.Geolocation.Geolocator();
                            location.getGeopositionAsync().then(
                                function (e) {
                                    //al obtener la localización creamos un objeto con la información
                                    var message = {
                                        msg: "coordinates",
                                        lat: e.coordinate.latitude,
                                        long: e.coordinate.longitude,
                                        alt: e.coordinate.altitude
                                    };
                                    //enviamos el mensaje al iframe que contiene el mapa
                                    mapFrame.postMessage(message, locationHost);
                                });
                        });

                })
                );

            //attach to returning messages from any frame...
            window.addEventListener("message", receiveMessage, false);
        }
    };

    function receiveMessage(e) {
        if (e.origin === "ms-appx-web://"+document.location.host) { //evita mensajes maliciosos
            switch (e.data.msg) {
                case "rightclick":
                    var appBar = document.querySelector("#appBar").winControl;
                    if (appBar.hidden) {
                        appBar.show();
                    }
                    else {
                        appBar.hide();
                    }
                    commands.innerText = JSON.stringify(e.data);
                    break;
                default:
                    //inside e.data comes the object sent using postMessage from the frame...
                    commands.innerText = JSON.stringify(e.data);
                    break;
            }
        }
    }

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    app.start();
})();
